package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.dao.mapper.web.KeywordNocopyMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.po.KeywordNocopy;
import com.wontube.smr.web.client.model.vo.KeywordNocopyItems;

/**
 * @author yuechao
 *
 */
public final class KeywordFilterDao {
	public static final String keyTypeMusic = "Music";
	public static final String keyTypeVideo = "Video";
	
	private KeywordFilterDao() {	
	}
	
	public static boolean isKeywordCopyright(String keyType, String keyword) {
		if (keyword == null) {
			return true;
		}
		
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("keyword_nocopy.key_type = ", keyType)
				.addCriterion("keyword_nocopy.keyword = ", keyword.toLowerCase());
			KeywordNocopyMapper mapper = session.getMapper(KeywordNocopyMapper.class);
			int count = mapper.countByExample(ex);
			
			return count < 1;
		} finally {
			session.close();
		}
	}
	
	public static KeywordNocopyItems listKeywordNocopy(String keyType, PagenateInfo pageInfo){
		KeywordNocopyItems items = new KeywordNocopyItems(pageInfo);
		QueryExample ex = new QueryExample();
		if( ! StringUtils.isNullOrEmpty(keyType)){
			ex.or().addCriterion("keyword_nocopy.key_type=", keyType);
		}
		
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			KeywordNocopyMapper mapper = session.getMapper(KeywordNocopyMapper.class);
			//list
			List<KeywordNocopy> nocopys = mapper.selectByExample(ex);
			items.setItems(nocopys);
			
			//count
			int count = mapper.countByExample(ex);
			items.setTotalCount(count);
			return items;
		} finally {
			session.close();
		}		
	}
	
	public static int addKeywordNocopy(KeywordNocopy nocopy){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			KeywordNocopyMapper mapper = session.getMapper(KeywordNocopyMapper.class);
			return mapper.insert(nocopy);
		} finally {
			session.close();
		}
	}
	
	public static int deleteKeywordNocopy(String id){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			KeywordNocopyMapper mapper = session.getMapper(KeywordNocopyMapper.class);
			return mapper.deleteByPrimaryKey(id);
		} finally {
			session.close();
		}
	}
}
